package com.one.group.controller;

import com.one.group.model.vo.MessageVo;
import com.one.group.result.Result;
import com.one.group.service.chatHistoryService;
import com.one.group.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * {@code @Create:}  2025-10-15  14 : 42
 * {@code @Author:} Cherry
 * {@code @ToAuthor:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */

@SuppressWarnings({"all"})
@RestController
@RequestMapping("ai/history")
public class ChatHistoryController {
    @Resource
    private chatHistoryService chatHistoryService;

    @Resource
    private ChatMemory chatMemory;

    @GetMapping("/{type}")
    public Result<List<String>> getChatIds(@PathVariable("type") String type) {
        return ResultUtil.success(chatHistoryService.getChatIds(type));
    }

    @GetMapping("/{type}/{chatId}")
    public Result<List<MessageVo>> getChatHistory(
            @PathVariable("type") String type,
            @PathVariable("chatId") String chatId) {
        List<Message> messages = chatMemory.get(chatId);
        if (messages.size() <= 0) {
            return ResultUtil.success(List.of());
        }
        return ResultUtil.success(messages.stream().map(MessageVo::new).toList());
    }
}
